1 00:00:00,810 --> 00:00:01,500 Hey there. 2 00:00:01,500 --> 00:00:02,190 Welcome. 3 00:00:02,190 --> 00:00:07,560 In this lecture, we'll be scripting our final trap, which is a shotgun hidden in a tree. 4 00:00:07,560 --> 00:00:12,060 When our player steps on a pressure plate, it's going to trigger the shotgun and kill our player. 5 00:00:12,060 --> 00:00:15,630 So if I go walk over here and trigger a pressure plate, there you go. 6 00:00:15,630 --> 00:00:17,610 The shotgun shoots and kills me. 7 00:00:18,000 --> 00:00:20,160 So let's go ahead and get started. 8 00:00:21,310 --> 00:00:22,780 Same procedure as before. 9 00:00:22,780 --> 00:00:27,640 I'll create a new module script in the handler section for my starter player scripts, and I'm going 10 00:00:27,640 --> 00:00:30,400 to call this my shotgun trap handler. 11 00:00:31,320 --> 00:00:32,160 Inside of here. 12 00:00:32,160 --> 00:00:38,520 We'll paste in that same template as before, and I'll rename this to Shotgun Trap Handler. 13 00:00:38,550 --> 00:00:40,380 We'll get rid of the start function. 14 00:00:40,380 --> 00:00:42,480 We'll grab the collection service. 15 00:00:46,190 --> 00:00:50,840 And then we'll go ahead and define the tag for this module. 16 00:00:50,840 --> 00:00:55,340 And the tag for our shotgun trap is just shotgun trap. 17 00:00:56,950 --> 00:01:00,610 And then we can go ahead and create the other module script to act as our class. 18 00:01:00,610 --> 00:01:04,660 I'm going to call this the Shotgun trap class. 19 00:01:05,350 --> 00:01:07,300 We'll paste in that class template. 20 00:01:07,300 --> 00:01:11,740 We'll rename it to Shotgun Trap Class. 21 00:01:11,740 --> 00:01:15,160 And then we can go ahead and require that class within our handler. 22 00:01:15,160 --> 00:01:21,190 So shotgun trap class is equal to require script dot shotgun trap class. 23 00:01:22,030 --> 00:01:25,630 And then we'll listen to the collection service get instance added signal. 24 00:01:25,630 --> 00:01:33,490 Pass our tag, connect a function, get the instance, and then use our shotgun trap class dot new constructor. 25 00:01:33,490 --> 00:01:35,440 On this instance, boom! 26 00:01:35,440 --> 00:01:36,880 We already know what to do. 27 00:01:37,870 --> 00:01:42,160 Then in here I'll rename this to our Shotgun Trap objects. 28 00:01:43,800 --> 00:01:45,960 And then we can go ahead and grab some services. 29 00:01:45,960 --> 00:01:49,770 One is going to be the sound service to grab that shotgun sound. 30 00:01:49,770 --> 00:01:52,350 We'll need the player service. 31 00:01:53,810 --> 00:01:58,910 And they're also going to get the tween service to tween that pressure plate up and down when a player 32 00:01:58,910 --> 00:01:59,600 touches it. 33 00:02:01,760 --> 00:02:04,220 Then we can go ahead and make a couple references. 34 00:02:04,220 --> 00:02:06,380 One is going to be for the local player. 35 00:02:06,380 --> 00:02:13,610 So players local player will make a reference to the shotgun shoot sound and that's in sound service 36 00:02:13,850 --> 00:02:16,160 SFX shotgun shoot. 37 00:02:17,530 --> 00:02:20,110 And then we'll also get the pressure plate sound. 38 00:02:20,110 --> 00:02:25,690 So pressure plate sound that's in sound service SFX dot pressure plate. 39 00:02:26,760 --> 00:02:31,740 Then inside of our constructor, we could go ahead and make a reference to our model SCL two model. 40 00:02:31,740 --> 00:02:38,400 We'll get the pressure plate and inside of our model that part is just called pressure plate. 41 00:02:39,100 --> 00:02:43,270 We'll also go ahead and get the, um. 42 00:02:44,370 --> 00:02:45,060 Emitter. 43 00:02:45,060 --> 00:02:48,360 There's a particle emitter for emitting a flash. 44 00:02:48,360 --> 00:02:50,070 So that's going to be our muzzle flash. 45 00:02:50,070 --> 00:02:54,210 So I'll call this muzzle flash emitter. 46 00:02:54,210 --> 00:02:56,280 And that's located inside of our model. 47 00:02:56,280 --> 00:02:57,840 We'll get the shotgun. 48 00:02:57,840 --> 00:03:02,580 And inside of the shotgun there's an attachment named Attachment Muzzle. 49 00:03:02,580 --> 00:03:06,480 And inside of that muzzle we have a muzzle flash emitter. 50 00:03:07,460 --> 00:03:10,820 And then we also have a light instance in there to like flicker. 51 00:03:10,820 --> 00:03:14,120 So it looks like the shotgun shot and lit up the area around it. 52 00:03:14,120 --> 00:03:15,350 So we'll just call this light. 53 00:03:15,350 --> 00:03:17,810 And that's equal to model dot shotgun. 54 00:03:18,170 --> 00:03:23,510 And inside of the attachment muzzle we also have a point light instance. 55 00:03:24,200 --> 00:03:28,820 And last but not least we can have a Debounce called on cooldown and set that equal to false. 56 00:03:29,300 --> 00:03:32,780 Then inside of our model we have another part called Detect Zone. 57 00:03:32,780 --> 00:03:38,780 We'll listen for when that gets touched, connect a function and get the other part that touched it. 58 00:03:40,390 --> 00:03:43,360 And then again, we'll see if a player touched that part. 59 00:03:43,360 --> 00:03:47,860 So players get player from character, pass other part parent. 60 00:03:48,340 --> 00:03:51,280 And if we do not have a player then we'll just return. 61 00:03:53,170 --> 00:03:59,140 Otherwise, we can go ahead and define a function for when the shotgun is triggered or the pressure 62 00:03:59,140 --> 00:04:00,010 plate is triggered. 63 00:04:00,490 --> 00:04:03,850 I'll create a function and I'll just call this triggered. 64 00:04:05,290 --> 00:04:09,670 And we'll call this function from this lambda function. 65 00:04:10,530 --> 00:04:11,820 Inside of our triggered function. 66 00:04:11,820 --> 00:04:15,180 What we can go ahead and do is check if we're on cooldown. 67 00:04:15,330 --> 00:04:18,240 If we are, then we'll return. 68 00:04:18,270 --> 00:04:24,930 Otherwise, set on cooldown equal to true and then we can go ahead and clone our pressure plate sound. 69 00:04:24,930 --> 00:04:26,310 So I'll just call it pressure sound. 70 00:04:26,310 --> 00:04:29,400 And that's equal to pressure plate sound clone. 71 00:04:29,550 --> 00:04:33,480 And we'll set play on remove equal to true. 72 00:04:33,840 --> 00:04:39,600 The parent is going to be equal to self dot model dot pressure plate. 73 00:04:40,630 --> 00:04:43,900 And then we'll just destroy the sound to play the pressure plate sound. 74 00:04:44,530 --> 00:04:48,490 And then afterwards, we want to go ahead and tween the pressure plate to move downwards to look like 75 00:04:48,490 --> 00:04:49,150 we stepped on it. 76 00:04:49,150 --> 00:04:54,820 So tween service will create a new tween on self dot model dot pressure plate. 77 00:04:54,820 --> 00:04:56,770 We'll do a tween info dot new. 78 00:04:56,800 --> 00:04:58,240 We'll play for like 0.5 seconds. 79 00:04:58,240 --> 00:05:00,280 It doesn't really matter how long you want to do it. 80 00:05:00,550 --> 00:05:08,650 And we're going to update the position of the pressure plate equal to self dot model, dot pressure 81 00:05:08,650 --> 00:05:10,510 plate dot position. 82 00:05:10,510 --> 00:05:13,360 And then we're going to offset it with a new vector three. 83 00:05:13,360 --> 00:05:18,520 And we're going to move it um minus one in the y direction. 84 00:05:19,810 --> 00:05:22,090 And then we can just play this tween. 85 00:05:22,090 --> 00:05:28,090 And then afterwards we can wait for the duration of this tween which is 0.5 seconds. 86 00:05:28,090 --> 00:05:34,000 Or we can also just store the tween in a variable and listen to when the event completes instead. 87 00:05:34,390 --> 00:05:35,410 Either way works. 88 00:05:35,410 --> 00:05:37,390 So we'll do it this way. 89 00:05:37,390 --> 00:05:41,560 So when tween dot completed fires, we're going to wait for that event. 90 00:05:43,090 --> 00:05:46,570 And then once that event fires, we're going to clone the shoot sound. 91 00:05:46,570 --> 00:05:48,610 So we'll call this shoot sound. 92 00:05:48,610 --> 00:05:52,120 And that's equal to shotgun shoot sound. 93 00:05:52,120 --> 00:05:54,580 Clone that shoot sound. 94 00:05:54,580 --> 00:05:57,700 We're going to set play on remove also equal to true. 95 00:05:58,390 --> 00:06:03,490 The parent is going to be equal to uh self dot model dot shotgun. 96 00:06:03,490 --> 00:06:05,890 And then we can just set it uh to the attachment. 97 00:06:05,890 --> 00:06:10,990 So attachment muzzle and then we'll destroy the sound instance. 98 00:06:11,710 --> 00:06:16,900 And then once we do that, we can go ahead and emit that muzzle flash from the particle emitter. 99 00:06:16,900 --> 00:06:22,060 So self dot muzzle flash emitter, we're going to emit one particle. 100 00:06:22,450 --> 00:06:26,020 And then we're also going to enable the light in our shotgun. 101 00:06:26,950 --> 00:06:32,170 And since we want this light to flicker on and then back off we can delay a function. 102 00:06:32,960 --> 00:06:38,570 For a very short duration, and that duration is going to be how long our muzzle flash exists for, 103 00:06:38,570 --> 00:06:41,720 so we can refer to our muzzle flash emitter. 104 00:06:42,900 --> 00:06:47,910 And then we can refer to the lifetime property, which is a number sequence, and then get the max value 105 00:06:47,910 --> 00:06:48,510 in there. 106 00:06:49,220 --> 00:06:55,550 And then once that time is up, we can go ahead and set self dot light dot enabled back equal to false. 107 00:06:56,420 --> 00:06:59,870 And then we also want to go ahead and kill the player. 108 00:06:59,870 --> 00:07:02,450 So local player. 109 00:07:03,280 --> 00:07:05,440 Dot character dot humanoid. 110 00:07:05,440 --> 00:07:11,380 We want to change the state equal to the enum dot humanoid state type dot dead. 111 00:07:12,610 --> 00:07:21,880 After all of that, then we can go ahead and, um, we can wait for, I guess, the shotgun shoot sound 112 00:07:21,880 --> 00:07:22,360 to end. 113 00:07:22,360 --> 00:07:26,110 So shotgun, uh, shoot sound. 114 00:07:26,110 --> 00:07:29,770 We'll get the time length, because it's going to be the exact same as our shoot sound. 115 00:07:29,770 --> 00:07:31,630 We're going to wait for that duration. 116 00:07:32,160 --> 00:07:33,900 And I want that duration is up. 117 00:07:33,900 --> 00:07:38,580 We'll create a tween back on our pressure plate to move it back to its original position. 118 00:07:39,150 --> 00:07:48,450 So we'll refer to self dot model dot pressure plates between info dot new 0.5 seconds. 119 00:07:48,630 --> 00:07:54,390 And then we're going to set the position of the pressure plate equal to self dot model dot pressure 120 00:07:54,390 --> 00:07:57,060 plate dot position. 121 00:07:57,270 --> 00:07:59,610 And then we can offset it again with a new vector three. 122 00:07:59,610 --> 00:08:04,320 But this time we're going to move it back to its original position which is one stud upwards in the 123 00:08:04,320 --> 00:08:05,340 y direction. 124 00:08:06,460 --> 00:08:12,490 And then we play that tween and then we can set self on cooldown back to false. 125 00:08:12,520 --> 00:08:13,840 Okay cool. 126 00:08:13,960 --> 00:08:19,540 So now any time our player goes and touches a pressure plate associated with a shotgun trap, we should 127 00:08:19,540 --> 00:08:20,980 move the pressure plate downwards. 128 00:08:20,980 --> 00:08:22,450 It'll play the shoot sound. 129 00:08:22,450 --> 00:08:25,480 It'll emit a flash of light and it'll emit a particle. 130 00:08:25,480 --> 00:08:26,440 It'll kill our player. 131 00:08:26,440 --> 00:08:28,060 And then it should reset itself. 132 00:08:28,060 --> 00:08:31,510 So it's time to test to see if it works. 133 00:08:31,510 --> 00:08:37,060 So we'll play test our game here, and I'll get back to you when I find a shotgun trap. 134 00:08:38,480 --> 00:08:41,450 Alrighty, here is one of our shotgun traps right there. 135 00:08:41,450 --> 00:08:44,960 And I believe the pressure plate is this stone right here. 136 00:08:44,960 --> 00:08:50,450 So if I go and walk and touch this plate here, it should move down and the shotgun should kill me. 137 00:08:50,450 --> 00:08:51,380 So if I step on it. 138 00:08:53,720 --> 00:08:57,770 It played the sound but unfortunately looked like nothing happened. 139 00:08:58,130 --> 00:09:01,610 So let's go ahead and see exactly what we did wrong. 140 00:09:01,610 --> 00:09:04,610 I don't think we played the tween, which is our issue. 141 00:09:04,640 --> 00:09:05,060 Yeah. 142 00:09:05,060 --> 00:09:08,300 So we forgot to play the tween right here. 143 00:09:08,300 --> 00:09:13,040 Make sure you actually play the tween before you wait to listen for the tween to complete. 144 00:09:13,040 --> 00:09:14,390 Very important to do. 145 00:09:14,510 --> 00:09:15,020 Okay. 146 00:09:15,020 --> 00:09:16,250 Let's go ahead and try that again. 147 00:09:16,250 --> 00:09:21,890 But this time to make our odds easier of finding one of these shotgun traps. 148 00:09:21,890 --> 00:09:25,820 We're just going to go ahead and increase the chance of, for example, this model. 149 00:09:25,820 --> 00:09:27,440 I'll just set it to like 100. 150 00:09:27,440 --> 00:09:31,040 So that way we're guaranteed of finding a shotgun trap on the first try. 151 00:09:31,040 --> 00:09:31,640 So there we go. 152 00:09:31,640 --> 00:09:32,810 There's a shotgun trap. 153 00:09:33,330 --> 00:09:36,090 And the pressure plate should be one of these stones over here. 154 00:09:36,090 --> 00:09:37,830 So I'll just walk around over here. 155 00:09:38,610 --> 00:09:39,720 There we go. 156 00:09:39,750 --> 00:09:43,800 The pressure plate went down and then we got shotgunned in our face. 157 00:09:43,800 --> 00:09:44,790 Very cool. 158 00:09:46,090 --> 00:09:47,620 So if we go. 159 00:09:48,460 --> 00:09:49,420 And try it again. 160 00:09:49,420 --> 00:09:52,090 Hopefully it should be reset and it should kill us again. 161 00:09:52,930 --> 00:09:53,620 There we go. 162 00:09:53,620 --> 00:09:58,030 Just like that, we've got our nice little hidden shotgun trap. 163 00:09:58,480 --> 00:09:59,110 All right. 164 00:09:59,110 --> 00:10:01,360 We're done with all of the traps for our project. 165 00:10:01,360 --> 00:10:04,420 We just have a couple more things to add and we'll be finished. 166 00:10:04,420 --> 00:10:06,700 So I'll see you in the next lecture.